

package com.uit.ecommerce.access.mapper;

import com.uit.ecommerce.dto.NewsDTO;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class NewsMapper extends MapperDB{
    public NewsMapper() throws Exception{
        super();
    }
private void InitNewsDTOFromRs(NewsDTO news, ResultSet rs) throws Exception{
        if((rs != null) && (news != null)){
            news.setId(rs.getString("MaBanTin"));
            news.setContents(rs.getString("NoiDung"));
            news.setDate(rs.getString("NgayDang"));
            news.setAuthor(rs.getString("NguoiDang"));
        }
    }
      public ArrayList<NewsDTO> GetAllNews() throws Exception{
        ArrayList<NewsDTO> listResult = new ArrayList<NewsDTO>();
        try{
            StringBuffer sql = new StringBuffer();
            sql.append("select * from ecormerce.BanTin");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           while((rs != null) && (rs.next())){
                NewsDTO temp = new NewsDTO();
                InitNewsDTOFromRs(temp, rs);
                listResult.add(temp);
            }
        }catch(Exception ex){
            throw ex;
        }

        return listResult;
    }
public void insertNews(NewsDTO cls) throws Exception{
    try{
            StringBuffer sql = new StringBuffer();
            sql.append("Insert into ecormerce.BanTin values('"+cls.getId()+"','"+cls.getContents()+"','"+cls.getDate()+"','"+cls.getAuthor()+"')");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            stmt.execute();
            stmt.close();
        }catch(Exception ex){
            throw ex;
        }
}
public void DeleteNews(String ID) throws Exception{
    try{
            StringBuffer sql = new StringBuffer();
            sql.append("Delete from ecormerce.BanTin where MaBanTin='");
            sql.append(ID).append("'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            stmt.execute();
            stmt.close();
        }catch(Exception ex){
            throw ex;
        }
}
public NewsDTO getNewInfo(String ID) throws Exception{
    NewsDTO Result = new NewsDTO();
        try{
            StringBuffer sql = new StringBuffer();
            sql.append("select * from ecormerce.BanTin where MaBanTin='"+ID+"'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           while((rs != null) && (rs.next())){
               InitNewsDTOFromRs(Result, rs);
              }
        }catch(Exception ex){
            throw ex;
        }
        return Result;
}
public ArrayList<NewsDTO> getNewByDate(String Date) throws Exception{
     ArrayList<NewsDTO> listResult = new ArrayList<NewsDTO>();
        try{
            StringBuffer sql = new StringBuffer();
            sql.append("select * from ecormerce.BanTin where NgayDang='"+Date+"'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           while((rs != null) && (rs.next())){
                NewsDTO temp = new NewsDTO();
                InitNewsDTOFromRs(temp, rs);
                listResult.add(temp);
            }
        }catch(Exception ex){
            throw ex;
        }

        return listResult;
}
public ArrayList<NewsDTO> getNewByAuthor(String Author) throws Exception{
     ArrayList<NewsDTO> listResult = new ArrayList<NewsDTO>();
        try{
            StringBuffer sql = new StringBuffer();
            sql.append("select * from ecormerce.BanTin where NguoiDang='"+Author+"'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           while((rs != null) && (rs.next())){
                NewsDTO temp = new NewsDTO();
                InitNewsDTOFromRs(temp, rs);
                listResult.add(temp);
            }
        }catch(Exception ex){
            throw ex;
        }

        return listResult;
}
public boolean checkNewsExit(String ID) throws Exception{
     try{
            StringBuffer sql = new StringBuffer();
            sql.append("select * from ecormerce.BanTin where MaBanTin='"+ID+"'");
             PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
           if((rs != null) && (rs.next())){
                return true;
            }
        }catch(Exception ex){
            throw ex;
        }
     return  false;
}
}
